In This Topic
Summary
There are a number of best practices, that will eliminate any chances of encountering problems in programming your applications.
Error checking
After each function or method call, you should always check for errors reported. Those can vary between invalid parameters, all the way up to functional or memory errors. There are various ways to check for errors depending on your preference and the functionality you are expecting to accomplish. The method in which you check for errors is illustrated in our many sample demo applications.
For example, if you want to check, that you have successfully displayed an image from a file, you can either:
- Check the value of the returned GdPictureStatus enumeration of the called Display method.
As you can see here, we checked the returned status, and if there was an error of any type, we display the error in a message box. The GetStat() method returns the status of the last function or method called.
The GdPictureImaging class and also the GdPicturePDF class contain the GetStat() methods that act accordingly. For TWAIN acquisition, please use GdPictureImaging.TwainGetState() instead.
- Check the status of the called Create or Load method using the GetStat() method first. You can subsequently check the image identifier, the valid value of the returned imageID should be different than 0.
Releasing your images
Memory is an essential topic in any imaging applications.
We all have encountered needs to deal with hundreds if not thousands of images, some of them very extreme, and if your program is not robust in handling the memory creation and release of those images, you can very quickly overcome your computer’s limit, either slowing your application or worse, crashing it. Thus, it is really important to release images you have created using GdPicture.NET once you have no use for them.
If you are going to release already used image, you can select the method according to the object you are working with.
- If you operate with the instance of the GdPictureImaging class, use the ReleaseGdPictureImage() method.
For example, you are creating a multipage TIFF image file. So you create a GdPictureImage for each new page. Once you add the created image to your multipage TIFF image file, you should release the image source before you create a new image for a new page. This should be done via the GdPictureImaging.ReleaseGdPictureImage() method, as it is shown in these code snippets.
As you can see, we used the GdPictureImaging.ReleaseGdPictureImage() method on each added page and on the multipage TIFF file once we finished using it.
- If you operate with the instance of the GdPicturePDF class, use the static GdPictureDocumentUtilities.DisposeImage() method.
For example, you are extracting images from your source PDF document. So you extract an image from the page using a GdPictureImage object. Once you add and draw the created image on the new page in the destination PDF document, you should release the image source before you create a new image. This should be done via the static GdPictureDocumentUtilities.DisposeImage() method, as it is shown in these code snippets.
As you can see, we used the static GdPictureDocumentUtilities.DisposeImage() method on each drawn image once we finished using it.
Closing multipage files, PDF documents and TWAIN sources
It is really important to close TWAIN sources and to close multipage files (if there is a need to, depends on the saving mechanism used, for more information, please review the TiffSaveAsMultiPageFile method documentation). As for PDF documents, it is important to close them in order to release their memory. If you work with images as a part of your files or documents, you have to release them separately (see the section above). Closing and disposing of files or documents do not release used images. So here is an example of creating a searchable PDF from an existing PDF.
Closing displayed documents in GdViewer
It is always a best practice to close the displayed document in GdViewer using the GdViewer.CloseDocument() method before you display a new one. If this document exists in another class, let's say an image in GdPictureImaging class, you also have to release it using the GdPictureImaging.ReleaseGdPictureImage() method if you are done using it.
TWAIN issues
TWAIN is a massive protocol. Problems arise from the fact that different manufacturers have to support it individually for their machinery. To handle those issues, we have created a step by step protocol for you to follow in order to figure out the source of your problem. 95% of cases are solved by following the steps described here: TWAIN Acquisition Issues